package com.youhome.controller;

import com.youhome.model.BCounselor;
import com.youhome.model.Dict;
import com.youhome.model.District;
import com.youhome.model.HousePropertySubType;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.service.DictService;
import com.youhome.service.DistrictService;
import com.youhome.service.HousePropertySubTypeService;
import com.youhome.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * Created by chen365 on 2018/09/21.
 */
@Api(tags = "公共字典")
@RestController
@RequestMapping(value = "/dicts")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DistrictService districtService;


    @Autowired
    private HousePropertySubTypeService housePropertySubTypeService;

    @ApiOperation(value = "列表", notes = "列表", response = Dict.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "父级ID(默认0)", required = true, paramType = "path")
    })
    @GetMapping(value = "/{id}")
    public Object list(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            id = 0L;
        }
        Dict record = new Dict();
        record.setPid(id);
        List<Dict> dicts = dictService.selectByBean(record);
        return new Result<>(ResultCode.SUCCESS, dicts);
    }

    @ApiOperation(value = "房源类型", notes = "房源类型", response = HousePropertySubType.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/house_type")
    public Object housePropertySubTypeList() {
        List<HousePropertySubType> housePropertySubTypes = housePropertySubTypeService.selectAll();
        return new Result<>(ResultCode.SUCCESS, housePropertySubTypes);
    }

    @ApiOperation(value = "行政区域", notes = "行政区域", response = District.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "pid", value = "行政区域父级ID(默认0)", required = true, paramType = "query")
    })
    @GetMapping(value = "/districts")
    public Object district(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        List<District> districts = districtService.selectByPid(pid);
        return new Result<>(ResultCode.SUCCESS, districts);
    }
}
